import { Component, Input, EventEmitter, Output, } from '@angular/core';
import {compareProps} from "../../utils/basic";

/** 还款简要组件
 */
@Component({
  selector: 'back-money-brief',
  templateUrl: 'back-money-brief.html'
})
export class BackMoneyBriefComponent {

  // 金额
  private money: string= '--';
  // 剩余时间
  private resDay: number;
  // 显示逾期
  private showOverdue: string;
  @Input() options: any;
  // padding大小
  @Input() showSmallPadding: boolean;
  // 点击事件
  @Output() clickHandler = new EventEmitter<any>();

  constructor() {}

  ngOnChanges(){
    if(this.options.progress.length< 1) return;
    this.init();
  }
  /*** 处理后台返回数据,映射到页面中
   * */
  private init(): void
  {
    const backTime= new Date(this.options.time).getTime();
    const oldArr= [...this.options.progress];
    const arr= oldArr.sort(compareProps('order')).reverse();
    debugger;
    if(arr.length===4 && arr[0].timeLimit && arr[0].loanTime)
    {
      const borrowTime= new Date(arr[0].loanTime).getTime();
      let subDay= String((backTime-borrowTime)/(1000 * 60 * 60 * 24));
      this.resDay= parseInt(arr[0].timeLimit)- parseInt(subDay);
      const reg= /还款(\d+(\.\d+)?)元/;
      const describe= arr[0].describe;
      if(reg.exec(describe)){
        this.money= RegExp.$1;
      }
      if(this.resDay<0){ // 逾期状态
        this.showOverdue= `你已逾期${-this.resDay}天,请尽快还款。`;
      }

    }
  }

  // 点击操作
  executeClick()
  {
    this.clickHandler.emit({
      value: this.options
    });
  }
}
